package com.sdk.paas;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sdk.paas.conts.PaasConts;
import com.sdk.paas.core.CodeResult;
import com.sdk.paas.core.enums.ErrorEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.Objects;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class PaasResult<T> extends CodeResult {
    private T data;
    private String reqId;

    public static String fail(ErrorEnum errorEnum) {
        CodeResult result = new CodeResult();
        result.setCode(errorEnum.getKey());
        result.setMsg(errorEnum.getDesc());
        return JSONObject.toJSONString(result);
    }

    public static String fail(Integer code, String msg) {
        CodeResult result = new CodeResult();
        result.setCode(code);
        result.setMsg(msg);
        return JSONObject.toJSONString(result);
    }

    @JsonIgnore
    public boolean isSuccess() {
        return Objects.equals(PaasConts.SUCCESS_CODE, getCode());
    }
    @JsonIgnore
    public boolean isFail() {
        return !isSuccess();
    }
}
